<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
  <meta charset="UTF-8">
  <title>添加管理员-WeAdmin Frame型后台管理系统-WeAdmin 1.0</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="../../../static/css/font.css">
  <link rel="stylesheet" href="../../../static/css/weadmin.css">
  <script src="../../../static/lib/layui/layui.js" charset="utf-8"></script>
  <script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-3.5.1.min.js"></script>
  <script>
    $(function () {
      $.get({url: "/role/getMsg",success: function (data) {
          $("#role").html(data)
        },async: false})
    })
  </script>
</head>

<body>
<div class="weadmin-body">
  <form class="layui-form">
    <div class="layui-form-item">
      <label for="L_username" class="layui-form-label">
        <span class="we-red">*</span>登录名
      </label>
      <div class="layui-input-inline">
        <input type="text" id="L_username" name="username" lay-verify="required"  autocomplete="off"
               class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label for="L_email" class="layui-form-label">
        <span class="we-red">*</span>手机
      </label>
      <div class="layui-input-inline">
        <input type="text" id="L_phone" name="phone" lay-verify="required|phone" autocomplete="" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label for="L_email" class="layui-form-label">
        <span class="we-red">*</span>邮箱
      </label>
      <div class="layui-input-inline">
        <input type="text" id="L_email" name="email" autocomplete="off" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label for="role" class="layui-form-label">
        <span class="we-red">*</span>角色
      </label>
      <div class="layui-input-inline">
        <select name="role" id="role" th:fragment="roleList">
          <option th:each="adminRole:${session.adminRoles}" th:value="${adminRole.roleId}">[[${adminRole.roleName}]]</option>
        </select>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">
      </label>
      <button class="layui-btn add" lay-filter="add" lay-submit="">修改</button>
      <input type="hidden" name="dataId" id="dataId" value="" />
    </div>
  </form>
</div>
<script>
  layui.extend({
    admin: '{/}../../static/js/admin'
  });
  layui.use(['form', 'admin', 'jquery', 'table', 'layer'], function () {
    var form = layui.form,
            admin = layui.admin,
            $ = layui.jquery,
            table = layui.table,
            layer = layui.layer;


    //页面初始化
    $(function () {
      setTimeout(function () {
        frameVal();
      }, 100);
    });
    var dataId
    function frameVal() {
      dataId = $('input[name="dataId"]').val();
      parent.layui.jquery("#adminList tr").each(function () {
        if ($(this).attr('data-id') == dataId) {
          var tdArr = $(this).children('td');
          var username = tdArr.eq(2).text();
          var phone = tdArr.eq(3).text();
          var email = tdArr.eq(4).text();
          var role = tdArr.eq(5).text();
          $('input[name="username"]').val(username);
          $('input[name="phone"]').val(phone);
          $('input[name="email"]').val(email);
          $.get({
            url:"/admin/getId/"+dataId,
            success: function (data) {
              $("option[value='"+data+"']").attr("selected",true)
            },
            async: false
          })
          form.render();
        }
      });
    }


    form.on('submit(add)', function () {
      var name = $("input[name='username']").val()
      var phone = $("input[name='phone']").val()
      var email = $("input[name='email']").val()
      var roleId = $("option:selected").val()
      var roleName = $("option:selected").html()
      var admin_data = {name: name,phone: phone,email: email,roleId: roleId}
      $.post({
        url: "/admin/update/"+dataId,
        contentType: "application/json;charset=UTF-8",
        data: JSON.stringify(admin_data),
        dataType: "json",
        success: function () {
          var tdArr = parent.layui.jquery("#roleList tr[data-id='"+dataId+"']").children("td")
          tdArr.eq(2).text(name);
          tdArr.eq(3).text(phone);
          tdArr.eq(4).text(email);
          tdArr.eq(5).text(roleName);
          form.render();
          },
        async: false
        })
      layer.alert("修改成功", {
        icon: 6
      }, function () {
        // 获得frame索引
        var index = parent.layer.getFrameIndex(window.name);
        //关闭当前frame
        parent.layer.close(index);
      })
      return false;
      })
    });

</script>
</body>

</html>